package tech.popsoft.cip.manage.service.impl;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.popsoft.cip.manage.mapper.ApiServicePermissionMapper;
import tech.popsoft.cip.manage.service.ApiAppService;
import tech.popsoft.cip.manage.service.ApiServicePermissionService;
import tech.popsoft.cip.manage.service.ApiServiceService;
import tech.popsoft.platform.common.base.BaseServiceImpl;
import tech.popsoft.cip.manage.entity.ApiServicePermission;

import java.util.List;

/**
 * 服务权限 服务实现类
 * @author wqliu
 * @date 2021-08-17
 *
 */
@Service
public class ApiServicePermissionServiceImpl extends BaseServiceImpl<ApiServicePermissionMapper, ApiServicePermission> implements ApiServicePermissionService {

    @Autowired
    private ApiServiceService apiServiceService;

    @Autowired
    private ApiAppService apiAppService;

    @Override
    public ApiServicePermission init() {
        ApiServicePermission entity=new ApiServicePermission();

        return entity;
    }

    @Override
    public boolean hasPermission(String appCode, String serviceCode) {

        //获取应用标识
        String appId = apiAppService.getByCode(appCode).getId();
        //获取服务标识
        String serviceId = apiServiceService.getByCode(serviceCode).getId();

        List<ApiServicePermission> list = this.lambdaQuery()
                .eq(ApiServicePermission::getAppId, appId)
                .eq(ApiServicePermission::getServiceId, serviceId)
                .list();
        if (CollectionUtils.isNotEmpty(list)) {
            return true;
        }

        return false;
    }
}
